{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f6aa394",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0ea6a8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"http://127.0.0.1:8000/\"\n",
    "username = \"vnpy\"\n",
    "password = \"vnpy\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70160e37",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取令牌\n",
    "r = requests.post(\n",
    "    url + \"token\",\n",
    "    data={\"username\": username, \"password\": password},\n",
    "    headers={\"accept\": \"application/json\"}\n",
    ")\n",
    "token = r.json()[\"access_token\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6f7379d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查询函数\n",
    "def query_test(name):\n",
    "    \"\"\"查询对应类型的数据\"\"\"\n",
    "    r = requests.get(\n",
    "        url + name,\n",
    "        headers={\"Authorization\": \"Bearer \" + token}\n",
    "    )\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "729a6ef1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 行情订阅\n",
    "r = requests.post(\n",
    "    url + \"tick/\" + \"cu2112.SHFE\",\n",
    "    headers={\"Authorization\": \"Bearer \" + token}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc7001e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 批量查询\n",
    "for name in [\"tick\", \"contract\", \"account\", \"position\", \"order\", \"trade\"]:\n",
    "    data = query_test(name)\n",
    "    print(name + \"-\" * 20)\n",
    "    if data:\n",
    "        print(data[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53b06a7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 委托测试\n",
    "req = {\n",
    "    \"symbol\": \"cu2109\",\n",
    "    \"exchange\": \"SHFE\",\n",
    "    \"direction\": \"多\",\n",
    "    \"type\": \"限价\",\n",
    "    \"volume\": 1,\n",
    "    \"price\": 69500,\n",
    "    \"offset\": \"开\",\n",
    "    \"reference\": \"WebTrader\"\n",
    "}\n",
    "\n",
    "r = requests.post(\n",
    "    url + \"order\",\n",
    "    json=req,\n",
    "    headers={\"Authorization\": \"Bearer \" + token}\n",
    ")\n",
    "vt_orderid = r.json()\n",
    "\n",
    "print(vt_orderid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3435c36e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 撤单测试\n",
    "r = requests.delete(\n",
    "    url + \"order/\" + vt_orderid,\n",
    "    headers={\"Authorization\": \"Bearer \" + token}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7341f278",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# Weboscket测试\n",
    "from websocket import create_connection\n",
    "\n",
    "ws = create_connection(\"ws://127.0.0.1:8000/ws/?token=\" + token)\n",
    "\n",
    "while True:\n",
    "    result =  ws.recv()\n",
    "    print(\"Received '%s'\" % result)\n",
    "\n",
    "ws.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c06cdf3d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cb0a41d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
